package com.fyx.controller;

import com.                                                                       alibaba.fastjson.JSONObject;
import com.fyx.annotation.Log;
import com.fyx.entity.*;
import com.fyx.service.IActiveDataService;
import com.fyx.utils.PageUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ResponseHeader;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.Map;
import java.util.Set;

@Api(tags = "活动数据")
@RestController
@RequestMapping("/api/activeData")
@RequiredArgsConstructor
public class ActiveDataController {
    @Autowired
    private IActiveDataService iActiveDataService;

    @Log("查询美团够30人数")
    @ApiOperation("查询美团够30人数")
    @GetMapping(value = "getMeituanReList")
    @PreAuthorize("@el.check('getMeituanRe:list')")
    public Map getMeituReList(MeituanReCount meituanReCount, Pageable pageable){
        Map map=iActiveDataService.getMeituReList(meituanReCount, pageable);
        return  map;
    }

    @Log("兑换话费")
    @ApiOperation("兑换话费")
    @PostMapping(value = "sendhf")
    @PreAuthorize("@el.check('telephoneCharge:convert')")
    public ResponseEntity<Object> sendhf(@Validated @RequestBody WithdrawEntity withdrawEntity) throws IOException {
        iActiveDataService.sendhf(withdrawEntity);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }


    @Log("查询邮件")
    @ApiOperation("查询邮件")
    @GetMapping(value = "gameEmail")
    @PreAuthorize("@el.check('gameEmail:list')")
    public ResponseEntity<Object> query(GameEmailEntity gameEmailEntity, Pageable pageable){
        Map map=iActiveDataService.gameEmail(gameEmailEntity, pageable);
        return new ResponseEntity<>(PageUtil.toPage(map.get("list"),map.get("total")), HttpStatus.OK);
    }

    @DeleteMapping(value = "delMail")
    @PreAuthorize("@el.check('gameEmail:list')")
    @Log("删除邮件")
    @ApiOperation("删除邮件")
    public ResponseEntity<Object> delMail(@Validated @RequestBody String id,String flag)  {
        try {
            if(iActiveDataService.delMail(id,flag)){
                return new ResponseEntity<>( HttpStatus.OK);
            }else {
                return new ResponseEntity<>( HttpStatus.BAD_REQUEST);
            }
        } catch (IOException e) {
            e.printStackTrace();
            return new ResponseEntity<>( HttpStatus.BAD_REQUEST);
        }
    }

    @Log("查询话费兑换")
    @ApiOperation("查询话费兑换")
    @GetMapping(value="telephoneCharge")
    @PreAuthorize("@el.check('telephoneCharge:list')")
    public ResponseEntity<Object> query(TelephoneChargeEntity telephoneChargeEntity, Pageable pageable){
        Map map=iActiveDataService.telephoneCharge(telephoneChargeEntity, pageable);
        return new ResponseEntity<>(PageUtil.toPage(map.get("list"),map.get("total")),HttpStatus.OK);
    }


    @Log("兑换话费")
    @ApiOperation("兑换话费")
    @PostMapping(value = "convert")
    @PreAuthorize("@el.check('telephoneCharge:convert')")
    public ResponseEntity<Object> convert(@Validated @RequestBody TelephoneChargeEntity telephoneChargeEntity) throws IOException {
       // iActiveDataService.convert(telephoneChargeEntity);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }



    @Log("活动列表")
    @ApiOperation("活动列表")
    @GetMapping(value="activeList")
    @PreAuthorize("@el.check('active:list')")
    public ResponseEntity<Object> activeList(ActivityListEntity activityListEntity, Pageable pageable){
        Map mapResult = iActiveDataService.getActiveList(activityListEntity,pageable);
        return new ResponseEntity<>(PageUtil.toPage(mapResult.get("list"),mapResult.get("total")), HttpStatus.OK);
    }

    @Log("添加活动")
    @ApiOperation("添加活动")
    @PostMapping(value="addActive")
    public ResponseEntity<Object> addActive(@Validated @RequestBody ActivityListEntity  activityListEntity){
        System.out.println(activityListEntity);
        if(iActiveDataService.addActive(activityListEntity)) {
            return new ResponseEntity<>(HttpStatus.OK);
        }else {
            return new ResponseEntity<>(HttpStatus.NO_CONTENT);
        }
    }

    @DeleteMapping(value = "delActive")
    @Log("删除活动")
    @ApiOperation("删除活动")
    public ResponseEntity<Object> delActive(@RequestBody Set<String> ids,String flag){

        for (String id : ids) {
            iActiveDataService.delActive(id,flag);
        }
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @PutMapping(value = "editActive")
    @Log("编辑活动")
    @ApiOperation("编辑活动")
    public ResponseEntity<Object> editActive(@Validated @RequestBody ActivityListEntity activityListEntity){

        if(iActiveDataService.editActive(activityListEntity)){
            return new ResponseEntity<>(HttpStatus.OK);
        }
        else{
            return new ResponseEntity<>(HttpStatus.NO_CONTENT);
        }
    }

    @PostMapping(value = "toActiveNotice")
    @Log("通知活动")
    @ApiOperation("通知活动")
    public ResponseEntity<Object> toActiveNotice(@RequestBody String id) throws IOException {
        if(iActiveDataService.toActiveNotice(id)){
            return new ResponseEntity<>(HttpStatus.OK);
        }
        else{
            return new ResponseEntity<>(HttpStatus.NO_CONTENT);
        }
    }

    @GetMapping(value = "getShare1")
    @ApiOperation("通知分享")
    public ResponseEntity<Object> getShare(Pageable pageable)  {
         Map map=iActiveDataService.getShare(pageable);
        return new ResponseEntity<>(PageUtil.toPage(map.get("list"),map.get("total")), HttpStatus.OK);
    }

    @GetMapping(value = "getShare")
   // @ApiOperation("通知分享")
    public ResponseEntity<Object> getShare1(String type)  {
        Map map=iActiveDataService.getShare1(type);
        return new ResponseEntity<>(PageUtil.toPage(map.get("list"),map.get("total")), HttpStatus.OK);
    }


    @PostMapping(value = "addShare")
    @Log("添加分享")
    @ApiOperation("添加分享")
    public ResponseEntity<Object> addShare(@RequestBody ShareEntity shareEntity)  {

        if(iActiveDataService.addShare(shareEntity)){
            return new ResponseEntity<>( HttpStatus.OK);
        }else {
            return new ResponseEntity<>( HttpStatus.NO_CONTENT);
        }

    }
    @DeleteMapping(value = "delShare")
    @Log("删除分享")
    @ApiOperation("删除分享")
    public ResponseEntity<Object> delShare(@RequestBody int id)  {

        if(iActiveDataService.delShare(id)){
            return new ResponseEntity<>( HttpStatus.OK);
        }else {
            return new ResponseEntity<>( HttpStatus.NO_CONTENT);
        }
    }

    @PutMapping(value = "editShare")
    @Log("删除分享")
    @ApiOperation("删除分享")
    public ResponseEntity<Object> editShare(@RequestBody ShareEntity shareEntity)  {

        if(iActiveDataService.editShare(shareEntity)){
            return new ResponseEntity<>( HttpStatus.OK);
        }else {
            return new ResponseEntity<>( HttpStatus.NO_CONTENT);
        }
    }

    @PostMapping(value = "doShare")
    @Log("通知分享")
    @ApiOperation("通知分享")
    public ResponseEntity<Object> doShare(@RequestParam("file") MultipartFile file)  {

        String resutl=iActiveDataService.doShare(file);
        JSONObject js=new JSONObject();
        js.put("flag",true);
        js.put("msg",resutl);
            return new ResponseEntity<>(js,HttpStatus.OK);

    }



}

